'use strict';

var debug = require('debug'),
  log = debug('yozh:mw:log'),
  error = debug('yozh:mw:error'),
  verbose = debug('yozh:mw:verbose');

/**
 * redirectSSL middleware
 * @param  {Object} opts options
 * @return {Function} ES6 generator function
 */
module.exports = function(opts) {
  var middleware = function *redirectSSL(next) {
    if (this.secure) {
      // use https mode;
      return yield next;
    }
    var httpsPort = opts.port || 443;
    // var urlObject = url.parse('http://' + this.request.header.host);
    // var httpsHost = opts.hostname || urlObject.hostname;
    var httpsHost = opts.hostname || this.request.hostname;
    if (!opts.temporary) {
      this.response.status = 301;
    }
    // redirect to https link;
    this.response.redirect('https://' + httpsHost + ':' +
      httpsPort + this.request.url);
  };
  return middleware;
};
